﻿#if DELETED

using Apewer;
using Apewer.Network;
using Apewer.Web;
using System;
using System.Collections.Generic;
using System.Text;

namespace Apewer.Internals
{

    internal class WebFromTrusted
    {

#region Instance

        private List<string> _presetips = new List<string>();

        public List<string> PresetIPs { get { return _presetips; } }

        public string ClientIP { get; private set; }

        public string Mode { get; private set; }

        public string RemoteIP { get; private set; }

        public string ProxyIP { get; private set; }

        public bool Trusted { get; private set; }

        internal WebFromTrusted(IEnumerable<string> hosts)
        {
            foreach (var domain in hosts) AddPreset(domain);
            ClientIP = PageUtility.ClientIP;
            Trusted = GetTrusted();
        }

        internal WebFromTrusted(params string[] hosts) : this((IEnumerable<string>)hosts) { }

        private bool AddPreset(string domain)
        {
            if (domain.IsEmpty()) return false;

            var ip = null as string;
            ip = NetworkUtility.IsIP(domain) ? domain : NetworkUtility.Resolve(domain);
            if (ip.IsEmpty()) return false;
            if (!NetworkUtility.IsIP(ip)) return false;

            if (!_presetips.Contains(ip)) _presetips.Add(ip);
            return true;
        }

        private bool GetTrusted()
        {
            var ips = ClientIP.Split(",");
            switch (ips.Length)
            {
                case 1:
                    {
                        Mode = "direct";
                        RemoteIP = ips[0].Split(":")[0];

                        // 信任局域网访问。
                        if (NetworkUtility.FromLAN(RemoteIP)) return true;

                        // 检测公网地址。
                        if (PresetIPs.Contains(RemoteIP)) return true;
                    }
                    break;
                case 2:
                    {
                        Mode = "proxy";
                        ProxyIP = ips[1].Split(":")[0];
                        RemoteIP = ips[0].Split(":")[0];

                        // 拒绝匿源代理。
                        if (RemoteIP.IsEmpty()) return false;

                        // 信任局域网中的代理。
                        if (NetworkUtility.FromLAN(ProxyIP)) return true;

                        // 信任公网远端。（CDN：Remote=Preset Proxy=CDN）
                        if (PresetIPs.Contains(RemoteIP)) return true;

#if DEBUG

                        // 信任预置 IP 代理。（本地调试：Remote=127.0.0.1 Proxy=Preset）
                        if (PresetIPs.Contains(ProxyIP)) return true;

#endif

                    }
                    break;
                default:
                    {
                        Mode = "other";
                    }
                    break;
            }
            return false;
        }

#endregion

#region Static Methods

        /// <summary>获取远端 IP。</summary>
        public static string GetRemoteIP()
        {
            var ip = PageUtility.ClientIP;
            var iof = ip.IndexOf(",");
            if (iof > -1) ip = ip.Substring(0, iof);
            ip = ip.Split(":")[0];
            return ip;
        }

#endregion

    }

}

#endif
